package com.zjsru.oneDay202205;

/**
 * @Author: likew
 * @Date: 2022/5/27
 *
 * 单词距离
 *
 * 输入：words = ["I","am","a","student","from","a","university","in","a","city"], word1 = "a", word2 = "student"
 * 输出：1
 *
 */
public class findClosest {
    public int findClosest(String[] words, String word1, String word2) {
        int low = -1,high = -1,len = words.length, ans = len;
        for (int i = 0; i < words.length; i++) {
            if(words[i].equals(word1)){
                low = i;
            }
            if(words[i].equals(word2)){
                high = i;
            }
            if(low != -1 && high != -1){
                ans = Math.min(Math.abs(low - high),ans);
            }
        }
        return ans;
    }
    
    public static void main(String[] args) {
        findClosest findClosest = new findClosest();
        String[] words = new String[]{"I","am","a","student","from","a","university","in","a","city"};
        String word1 = "a";
        String word2 = "student";
        System.out.println(findClosest.findClosest(words, word1, word2));
    }
}
